package com.zc.im.controller;

import com.google.gson.Gson;
import com.zc.im.model.im.group.ZcImGroup;
import com.zc.im.model.im.group.ZcImGroupAudit;
import com.zc.im.model.im.group.ZcImGroupMember;
import com.zc.im.service.im.group.ImGroupAuditService;
import com.zc.im.service.im.group.ImGroupMemberService;
import com.zc.im.service.im.group.ImGroupService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 *群申请审批的Controller
 * Created by wangcc on 2017/7/8.
 */
@Controller
@RequestMapping(value = "/groupAudit")
public class ImGroupAuditController {

    @Autowired
    private ImGroupAuditService imGroupAuditService;
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(ImGroupAuditController.class);

    /**
     * 群审批添加方法
     * 传入的json为{"group_name":"群组名称","company_id":"公司id","created_user_id":"创建用户ID"}
     * @param groupAudit
     * @return
     */
    @RequestMapping(value = "/getguopAudit",method = RequestMethod.POST)
    public ResponseJson insertGroupAudit(String groupAudit){
        ResponseJson resposeJson = new ResponseJson();
        try {
            if (StringUtils.isEmpty(groupAudit)){
                return resposeJson;
            }
            Gson gson = new Gson();
            ZcImGroupAudit zcImGroupAudit =gson.fromJson(groupAudit, ZcImGroupAudit.class);
            Integer key = imGroupAuditService.insertGroupAudit(zcImGroupAudit);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("key", key);
            resposeJson.setDataMap(dataMap);
        }catch (Exception e){
          logger.error(e.getMessage(),e);
        }
        return resposeJson;
    }

    /**
     * 审批处理 pass通过 创建群。并把申请人加入该群内 refuse 不通过更新审批表
     * 传入的json为{"created_user_id":"创建用户ID","audit_user_id":"审批的客服ID","audit_content":"内容","audit_status":"审批状态 通过pass 拒绝 refuse"}
     * @param audit
     * @return
     */
    @RequestMapping(value = "/auditGroup",method = RequestMethod.POST)
    public ResponseJson auditGroup(String audit){
        ResponseJson responseJson=new ResponseJson();
        if (StringUtils.isEmpty(audit)){
            return responseJson;
        }
        ZcImGroupAudit imGroupAudit = JsonUtils.strToClass(audit,ZcImGroupAudit.class);
        //判断是否通过 pass通过 refuse 不通过
        Integer backKey = imGroupAuditService.auditGroup(imGroupAudit);
        Map<String ,Object> dataMap=new HashMap<>();
        dataMap.put("key",backKey);
        responseJson.setDataMap(dataMap);
        return responseJson;
    }
}


